var md5 = require("../MD5.js")
var checkNetWork = require("../CheckNetWork.js")
var config = require("../config.js")
var broadcast = require("../../utils/broadcast.js")
const app = getApp()
Page({
  data: {
    hidden: false,
    nocancel: true,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    getCodeBtnProperty: {
      titileColor: '#B4B4B4',
      disabled: false,
      loading: false,
      title: '获取验证码'
    },
    loginBtnProperty: {
      disabled: true,
      loading: false,
    },
    getCodeParams: {
      token: 'airbike-token',
      mobile: '',
      checksum: '',
    },
    registerParams: {
      mobile: '',
      code: '',
      checksum: ''
    },
    codeTfFocus: false,
    AirBikeUrl: {
      getcode: "https://www.easy-mock.com",
      register: "https://www.easy-mock.com"
    },
    //校验码
    SALT: "AIRBIKESALT",
  },
  onLoad: function (options) {
    // 生命周期函数--监听页面加载
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {
              console.log(res.userInfo)
            }
          })
        }
      }
    })
    
  },
  //输入手机号
  phoneTfInput: function (e) {
    var that = this
    var inputValue = e.detail.value
    var length = e.detail.value.length
    if (length == 11) {
      //给接口的mobile参数赋值,以及改变获取验证码的状态
      console.log(inputValue)//手机号
      that.setData({
        'getCodeParams.mobile': inputValue,
        'registerParams.mobile': inputValue,
        'getCodeBtnProperty.titileColor': '#34B5E3',
        'getCodeBtnProperty.disabled': false
      })
    } else {
      //给接口的mobile参数赋值,以及改变获取验证码的状态
      that.setData({
        'getCodeParams.mobile': '',
        'registerParams.mobile': '',
        'getCodeBtnProperty.titileColor': '#B4B4B4',
        'getCodeBtnProperty.disabled': true
      })
    }
  },

  //获取验证码
  getCodeAct: function () {
    //请求接口
    if (checkNetWork.checkNetWorkStatu() == false) {
      console.log('网络错误')
    } else {
      var that = this
      var checksum = that.data.getCodeParams.token + that.data.getCodeParams.mobile + that.data.SALT
      var checksumMd5 = md5.hexMD5(checksum)
      that.setData({
        'getCodeParams.checksum': checksumMd5,
        //显示loading
        'getCodeBtnProperty.loading': true
      })
      wx.request({
        // url: "https://www.easy-mock.com/mock/594f7c129adc231f3569c4ad/example/code",
        url: config.url_all + '/getPinSms',
        data: {
          'phone': that.data.getCodeParams.mobile
        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        // header: {}, // 设置请求的 header
        // header: {
        // 'content-type': 'application/x-www-form-urlencoded'
        // },
        success: function (res) {
          var message = res.data.message
          var type = res.data.type
          if (type === 'SUCCESS') {
            wx.showToast({
              title: '已发送',
              icon: 'success',
              duration: 2000,
            })
            //启动定时器
            var number = 60;
            var time = setInterval(function () {
              number--
              that.setData({
                'getCodeBtnProperty.title': number + '秒',
                'getCodeBtnProperty.disabled': true
              })
              if (number == 0) {
                that.setData({
                  'getCodeBtnProperty.title': '重新获取',
                  'getCodeBtnProperty.disabled': false
                })
                clearInterval(time)
              }
            }, 1000)
          } else {
            wx.showToast({
              title: '发送失败',
              duration: 2000
            })
          }
          //光标下移
          that.setData({
            'codeTfFocus': true
          })
        },
        fail: function (res) {
          // fail
          console.log(res)
          that.failMessage()
        },
        complete: function () {
          // complete
          //隐藏loading
          that.setData({
            'getCodeBtnProperty.loading': false
          })
        }
      })
    }
  },

  //输入验证码
  codeTfInput: function (e) {
    var that = this
    var inputValue = e.detail.value
    var length = e.detail.value.length
    if (length == 6) {
      //给接口的mobile参数赋值,以及改变获取验证码的状态
      that.setData({
        'loginBtnProperty.disabled': false,
        'registerParams.code': inputValue
      })
    } else {
      //给接口的mobile参数赋值,以及改变获取验证码的状态
      that.setData({
        'loginBtnProperty.disabled': true,
        'registerParams.code': ''
      })
    }
  },

  // 绑定手机号码
  loginAct: function () {
    //光标取消
    var that = this
    that.setData({
      'codeTfFocus': true
    })
    //请求接口
    if (checkNetWork.checkNetWorkStatu() == false) {
      console.log('网络错误')
    } else {
      var checksum = that.data.registerParams.mobile + that.data.registerParams.code + that.data.SALT
      var checksumMd5 = md5.hexMD5(checksum)
      that.setData({
        'registerParams.checksum': checksumMd5,
        // 显示loading
        'loginBtnProperty.loading': true
      })
      wx.request({
        url: config.url_all + '/api/sys/account/updatePhone',
        header: {
          'Rememberme': wx.getStorageSync('token')
        },
        data: {
          'phone': that.data.getCodeParams.mobile,
          'pin': that.data.registerParams.code
        },
        success: function (res) {
          console.log(res.data)
          var message = res.data.message
          var type = res.data.type
          //if (getCodeParams.mobile)
          if (type == 'SUCCESS') {
            getApp().globalData.userInfo.phone = that.data.registerParams.mobile
            //判断是不是运营商
            wx.request({
              url: config.url_all + '/api/getUserinfo',
              header: {
                'Rememberme': wx.getStorageSync('token'),
                'content-type': 'application/x-www-form-urlencoded'
              },
              method: 'POST',
              success: function (res) {
                var roleName = res.data.data.roleList[0].roleName//用户角色
                console.log(roleName)
                if (roleName == "user") {
                  wx.showToast({
                    title: '绑定失败',
                  })
                } else if (roleName == "operator") {
                  wx.navigateTo({
                    url: '../information/information',
                  })
                }}
            })
          } else if (type == 'ERROR') {
            //需要在这判断已经注册的手机号是不是供应商，不是进行提示
            wx.showModal({
              title: '提示',
              content: message,
              showCancel: false
            })
          }
        },
        fail: function () {
          that.failMessage()
        },
        complete: function () {
          // 隐藏loading
          that.setData({
            'loginBtnProperty.loading': false
          })
        }
      })
    }
  },
  failMessage: function () {
    wx.showToast({
      title: '连接服务器失败',
      icon: 'loading',
      duration: 2000,
    })
  }
})